package com.lingnan.tijian.service.impl;

import com.lingnan.tijian.domain.Users;
import com.lingnan.tijian.dto.ApiResponse;
import com.lingnan.tijian.exception.TijianAPPExceptionEnum;
import com.lingnan.tijian.exception.TijianAppException;
import com.lingnan.tijian.mapper.UsersMapper;
import com.lingnan.tijian.service.ifac.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UsersServiceImpl implements UsersService {

    @Autowired
    UsersMapper usersMapper;

    /**
     * 处理注册业务
     */
    public ApiResponse registerService(Users user){
        user.setUserType(1);//通过手机注册，默认是一般用户，不是东软员工
        usersMapper.insertSelective(user);
        return new ApiResponse(ApiResponse.SUCCESS_STATUS,ApiResponse.SUCCESS_DESC);
    }

    public ApiResponse login(Users user){
        //  select * from users where userId=? and password=?   错
        // select * from users where userId=?  对
        Users userResult=usersMapper.selectByPrimaryKey(user.getUserId());
        System.out.println("登录方法中输出查询结果userResult:"+userResult);
        //看有没有注册账号
        if(userResult==null){
           // throw new RuntimeException();
            throw new TijianAppException(TijianAPPExceptionEnum.PHONE_NOT_REGISTER.getCode(),TijianAPPExceptionEnum.PHONE_NOT_REGISTER.getMessage());
        }
        System.out.println(userResult.getPassword()+"---"+user.getPassword());
        //看账号合不合法
        if(!userResult.getPassword().equals(user.getPassword())){
           // throw new RuntimeException();
            throw new TijianAppException(TijianAPPExceptionEnum.PASSWORD_IS_WRONG.getCode(),TijianAPPExceptionEnum.PASSWORD_IS_WRONG.getMessage());
        }

        return new ApiResponse(ApiResponse.SUCCESS_STATUS,ApiResponse.SUCCESS_DESC,userResult);


    }












}
